package user;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import replicant2.MyDatagramSocket;

public class FrontE extends corbainterfacePOA {
	public static final String CREATE = "0";
	public static final String SIGNIN = "1";
	public static final String SIGNOUT = "2";
	public static final String STATUS = "3";
	public static final String TRANSFER = "4";
	public static final String SUSPEND = "5";
	
	private MyDatagramSocket _mySocket = null;
	public FrontE(){
		try {
			 _mySocket = new MyDatagramSocket(9999);
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
	
	
	@Override
	public String createPlayerAccount(String FirstName, String LastName, String Age, String Username, String Password, String IPAddress) {

		final Object lock = new Object();
		String i = null;
		String details=null;
		try{
				synchronized(lock){
					details = CREATE+"#"+FirstName+"#"+LastName+"#"+Age+"#"+Username+"#"+Password+"#"+IPAddress+"#";
					System.out.println(details);
	 i = sendUDP_request(details);
	

				}
		}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		return i;
	}

	@Override
	public String playerSignIn(String Username, String Password, String IPAddress) {
		final Object lock = new Object();
		String i = null;
		String details=null;
		try{
				synchronized(lock){
					details = SIGNIN+"#"+Username+"#"+Password+"#"+IPAddress+"#";
					System.out.println(details);
	 i = sendUDP_request(details);
	

				}
		}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		return i;
	}

	@Override
	public String playerSignOut(String Username, String IPAddress) {
		final Object lock = new Object();
		String i = null;
		String details=null;
		try{
				synchronized(lock){
					details = SIGNOUT+"#"+Username+"#"+IPAddress+"#";
	 i = sendUDP_request(details);
	

				}
		}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		return i;
	}

	@Override
	public String getPlayerStatus(String Username, String Password,String IPAddress) {
		String i=null;
		String details=null;
		final Object lock = new Object();
		try{
				synchronized(lock){
	details = STATUS+"#"+Username+"#"+Password+"#"+IPAddress+"#";
	 i = sendUDP_request(details);
	

				}
		}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		return i;
	}

	@Override
	public String transferAccount(String Username, String Password,String OldIPAddress, String NewIPAddress) {
		final Object lock = new Object();
		String i = null;
		String details=null;
		try{
				synchronized(lock){
					details = TRANSFER+"#"+Username+"#"+Password+"#"+OldIPAddress+"#"+NewIPAddress+"#";
	 i = sendUDP_request(details);
	

				}
		}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		return i;
	}

	@Override
	public String suspendAccount(String AdminUsername, String AdminPassword,String AdminIP, String UsernameToSuspend) {
		final Object lock = new Object();
		String i = null;
		String details=null;
		try{
				synchronized(lock){
					details = SUSPEND+"#"+AdminUsername+"#"+AdminPassword+"#"+AdminIP+"#"+UsernameToSuspend+"#";
					
	 i = sendUDP_request(details);
	

				}
		}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		return i;
	}
	public String sendUDP_request(String details)
	{
		
		String ret = "";
		try {
			
			InetAddress receiverHost = InetAddress.getByName("132.205.45.214");
			System.out.println("front end send:"+details);
			_mySocket.sendMessage(receiverHost, 6781, details);
			ret =  _mySocket.receiveMessage();
			
			System.out.println("front end send receive:"+ret);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ret;
		
	}
	
	}


